package com.joyoung.sdk.resolve;

import android.content.Context;
import android.util.Log;

import com.joyoung.sdk.config.Cons_Sdk;

import org.dom4j.DocumentException;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/***
 * @author liuwei
 *         解析指令
 */
public class ResolveCommand {
    private JxXML jxXML;
    /**
     * @param cmd 设备返回的原始指令
     * @return ListOrderedMap  根据配置文件返回的有序的map键值对
     * @throws IOException
     * @throws DocumentException
     * @throws JSONException
     */
    public JSONObject resolve(Context context,ArrayList cmd) throws IOException, DocumentException, JSONException {
        int number = 10;
        JSONObject jsonObject = new JSONObject();
        if (jxXML == null)
            jxXML = new JxXML(context);
        LinkedHashMap listOrderedMap =jxXML.readFromAssets(Cons_Sdk.configXmlName);
        Iterator<String> keys = listOrderedMap.keySet().iterator();

        while (keys.hasNext()) {
            String key = (String) keys.next();
            if (key.equals("scrap")){
                Map map= (Map) listOrderedMap.get(key);
                String value=map.get(key).toString();
                int count = Integer.parseInt(value);
                number+=count;
            }else {
                Map map= (Map) listOrderedMap.get(key);
                String value=map.get(key).toString();
                Log.e("value",value);
                int count = Integer.parseInt(value);
                List newList=getSubListPage(cmd,number,count);

                String values = resolvebyte(newList);
                jsonObject.put(key, values);
                number += count;
            }
        }
        return jsonObject;

    }

    /***
     ***从一个byte[]数组中截取一部分
     * @param list   需要截取的byte[]
     * @param skip 从多少位开始截取
     * @param pageSize 截取的长度
     * @return 截取之后的byte[]
     */
    private List getSubListPage(List<String> list, int skip,
                                int pageSize) {
        if (list == null || list.isEmpty()) {
            return null;
        }
        int startIndex = skip;
        int endIndex = skip + pageSize;
        if (startIndex > endIndex || startIndex > list.size()) {
            return null;
        }
        if (endIndex > list.size()) {
            endIndex = list.size();
        }
        return list.subList(startIndex, endIndex);
    }

    private String resolvebyte(List list) {
        if (list == null)
            throw new NullPointerException("list 集合不能为null");
        String value = "";
        int len = list.size();
        for (int i = 0; i < len; i++) {
            value += list.get(i);
        }
        if (value.equalsIgnoreCase("00b2")||value.equalsIgnoreCase("5000")) {
            return value;
        }else {
            try {
                return String.valueOf(Long.parseLong(value,16));
            } catch (NumberFormatException e) {
                e.printStackTrace();
                return "";
            }
        }
    }
}
